package com.mashibing.juc.c_026_00_interview.A1B2C3;

import java.time.LocalTime;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

public class T02_00_LockSupport {

    public static void main(String[] args) {

        FIFOMutex fifoMutex = new FIFOMutex();

        Thread aThread = new Thread(() -> {
            try {
                System.out.printf("\033[32m %s ===>> brfore park() ===>> %s \033[0m\n",
                        LocalTime.now(), Thread.currentThread().getName());

                fifoMutex.lock();
                TimeUnit.MILLISECONDS.sleep(5555);

                System.out.printf("\033[32m %s ===>> after park() ===>> %s \033[0m\n",
                        LocalTime.now(), Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                fifoMutex.unlock();
            }
        }, "A");
        aThread.start();

        Thread bThread = new Thread(() -> {
            try {
                System.out.printf("\033[32m %s ===>> brfore park() ===>> %s \033[0m\n",
                        LocalTime.now(), Thread.currentThread().getName());
                fifoMutex.lock();
                System.out.printf("\033[32m %s ===>> after park() ===>> %s \033[0m\n",
                        LocalTime.now(), Thread.currentThread().getName());
            } finally {
                fifoMutex.unlock();
            }
        }, "B");
        bThread.start();

    }

    public void testBlocker() {
        LockSupport.park(this);
    }

    public static void main2(String[] args) throws Exception {
        T02_00_LockSupport t02_00_lockSupport = new T02_00_LockSupport();
        System.out.println(t02_00_lockSupport);

        Thread aThread = new Thread(() -> {
            System.out.printf("\033[32m %s ===>> brfore park() ===>> %s \033[0m\n",
                    LocalTime.now(), Thread.currentThread().getName());
//            LockSupport.park();
//            t02_00_lockSupport.testBlocker();
            LockSupport.parkUntil(System.currentTimeMillis() + 1234);
            System.out.printf("\033[32m %s ===>> after park() ===>> %s \033[0m\n",
                    LocalTime.now(), Thread.currentThread().getName());
        }, "A");
        aThread.start();

        TimeUnit.MILLISECONDS.sleep(1234);

        System.out.println(LockSupport.getBlocker(aThread));
//        aThread.interrupt();

    }

    static Thread t1 = null, t2 = null;
    public static void main1(String[] args) throws Exception {
        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();

        t1 = new Thread(() -> {
            for (char c : aI) {
                LockSupport.park(); // T1阻塞
                System.out.print(c); // T1苏醒并打印
                LockSupport.unpark(t2); // 叫醒T2
            }
        }, "t1");

        t2 = new Thread(() -> {
            for (char c : aC) {
                System.out.print(c); // T2苏醒并打印
                LockSupport.unpark(t1); // 叫醒t1
                LockSupport.park(); // T2阻塞
            }
        }, "t2");

        t1.start();
        t2.start();
    }

}


